<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: AddCart.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\cart;

use app\api\model\dao\Cart;
use app\api\model\dao\Goods;
use app\api\model\dao\GoodsSpec;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class AddCart extends PageBase
{

    private $objCart;
    private $objGoods;
    private $objGoodsSpec;

    public function __construct()
    {
        parent::__construct();
        $this->objCart = new Cart();
        $this->objGoods = new Goods();
        $this->objGoodsSpec = new GoodsSpec();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'goods_id' => 'require',
            'goods_number' => 'require',
            'goods_spec_id' => 'require',
        ];
        $msg = [
            'goods_id.require' => '商品ID必填',
            'goods_number.require' => '商品数量必填',
            'goods_spec_id.require' => '商品规格必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $user_id = $this->user_id;
        $goods_id = $param['goods_id'];
        $goods_spec_id = $param['goods_spec_id'];

        $goods = $this->objGoods->getGoods($goods_id);
        if (empty($goods)) {
            return $this->getRes(Error::ERR_SYS);
        }

        // 验证库存
        $goods_number_selected = $param['goods_number'];
        $goods_number = $goods[0]['goods_number'];
        if ($goods_number_selected > $goods_number) {
            return $this->getRes(Error::ERROR_CART_GOODS_NUMBER_ERROR);
        }

        $goods_spec_id = $param['goods_spec_id'];
        $goodsSpec = $this->objGoodsSpec->getGoodsSpecByid($goods_spec_id);
        if (empty($goodsSpec)) {
            return $this->getRes(Error::ERR_SYS);
        }
        $spec_goods_number = $goodsSpec[0]['spec_goods_number'];

        // 构造参数
        $input = array(
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'goods_name' => $goods[0]['goods_name'],
            'market_price' => $goods[0]['market_price'],
            'shop_price' => $goods[0]['shop_price'],
            'goods_number' => $goods_number_selected,
            'goods_spec_id' => $goods_spec_id,
            'goods_spec_name' => $goodsSpec[0]['spec_name'],
            'is_hot' => $goods[0]['is_hot'],
            'is_new' => $goods[0]['is_new'],
            'is_promote' => $goods[0]['is_promote'],
            'is_free_shipping' => $goods[0]['is_free_shipping'],
            'is_special_packing' => $goods[0]['is_special_packing'],
        );

        $ret = $this->objCart->getCartGoods($user_id, $goods_id, $goods_spec_id);
        if (!empty($ret)) {
            $cart_goods = $ret[0];
            $cart_goods_number = $cart_goods['goods_number'];
            $cart_goods_number_sum = $cart_goods_number + $goods_number_selected;
            if ($cart_goods_number_sum > $goods_number
                || $cart_goods_number_sum > $spec_goods_number
            ) {
                return $this->getRes(Error::ERROR_CART_GOODS_NUMBER_ERROR);
            }

            $ret = $this->objCart->editCart($user_id, $goods_id, $goods_spec_id, $cart_goods_number_sum);
            if ($ret === false) {
                return $this->getRes(Error::ERR_SYS);
            }
            return $this->getRes();
        }

        $ret = $this->objCart->addCart($input);
        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
